package com.yuhs.zixing.study.design.pattern.dp001.principle.p06.demeter;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 迪米特法则001
 * 有一个学校，下属有各个学院和总部，先要求打印出学校总部员工Id和学院员工ID
 * 编程实现上面的功能
 * @author: yuhs
 * @date 2022/3/26 下午9:24
 */
public class DemeterDemo001 {
    public static void main(String[] args) {
        SchoolManager001 schoolManager = new SchoolManager001();
        schoolManager.printAllEmployee(new CollegeManager001());

    }
}

/**
 * 学校总部员工类
 */
class Employee001 {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

/**
 * 学院员工类
 */
class CollegeEmployee001 {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

/**
 * 管理学院员工的管理类
 */
class CollegeManager001 {
    public List<CollegeEmployee001> getAllEmployee() {
        List<CollegeEmployee001> list = new ArrayList<>();
        CollegeEmployee001 employee001 = null;
        for (int i = 0; i < 10; i++) {
            employee001 = new CollegeEmployee001();
            employee001.setId("学校学院员工ID= " + i);
            list.add(employee001);
        }
        return list;
    }
}

/**
 * 管理学院员工的管理类
 */
class SchoolManager001 {
    public List<Employee001> getAllEmployee() {
        List<Employee001> list = new ArrayList<>();
        Employee001 employee001 = null;
        for (int i = 0; i < 10; i++) {
            employee001 = new Employee001();
            employee001.setId("学校总部员工ID= " + i);
            list.add(employee001);
        }
        return list;
    }

    /**
     * 输出学校总部和学院的信息
     *
     * @param collegeManager001
     */
    public void printAllEmployee(CollegeManager001 collegeManager001) {
        // 分析问题
        // 1、这里 CollegeManager001 不是 SchoolManager001 的直接朋友
        // 2、CollegeManager001 是以局部变量方式出现在 SchoolManager001
        // 3、违反了迪米特法则
        List<CollegeEmployee001> allEmployee = collegeManager001.getAllEmployee();
        System.out.println("-------------学院员工开始------------------");
        allEmployee.forEach(collegeEmployee001 -> System.out.println(collegeEmployee001.getId()));
        System.out.println("-------------学院员工结束------------------");

        List<Employee001> employee001List = this.getAllEmployee();
        System.out.println("-------------总部员工开始------------------");
        employee001List.forEach(employee001 -> System.out.println(employee001.getId()));
        System.out.println("-------------总部员工结束------------------");
    }
}
